import 'package:get/get.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:xin_he/utils/xin_he_storage.dart';

import '../../../routes/xin_he_routes.dart';
import '../../../service/xin_he_reqest.dart';
import '../../../utils/xin_he_toast.dart';
import '../model/xin_he_adopt_tree_model_entity.dart';
import 'xin_he_adopt_num_select_state.dart';

class XinHeAdoptNumSelectLogic extends GetxController {
  final XinHeAdoptNumSelectState state = XinHeAdoptNumSelectState();
  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
  }


  handleCount(bool isAdd){
    if(isAdd){
      state.count.value ++;
    }else{
      if(state.count.value == 0) return;
      state.count.value --;

    }
  }


  // requestAdoptOrdersCommit() async {
  //   var userId =  await XinHeLocalStorage.get(XinHeStorage.userId);
  //   Map<String,dynamic> ss = {
  //     'userId':userId,
  //     'adoptedTreeId': 524827956560789504,
  //     'amount':state.count.value,
  //     'buyerRemark':'',
  //
  //   };
  //
  //   XinHeRequestDataResult result = await XinHeHttpRequestData.requestUniversalPost(url: '/adoptedTreeOrder/saveAdoptedTreeOrder',params: ss,isPost: true);
  //   if(!result.result || result.data['code'] != 0) {
  //     XinHeToast.center(result.data['message']);
  //     return ;
  //   }
  //
  //
  // }

  textAlipay() async{
    XinHeRequestDataResult result = await XinHeHttpRequestData.requestUniversalPost(url: '/huifu/zhifubaoNative',isPost: true, isPostAndwwwForm: true);
    if(!result.result || result.data['code'] != 0) {
      XinHeToast.center(result.data['message']);
      return ;
    }
    // https://qr.alipay.com/bax02243gimokcdwqoqe556c

    launchWebUrl('alipays://platformapi/startapp?saId=10000007&qrcode=' + result.data['data']);


  }

  Future<void> launchWebUrl(String url) async {
    final Uri uri = Uri.parse(url);
    if (await canLaunchUrl(uri)) {
      await launchUrl(uri, mode: LaunchMode.externalApplication);
    } else {
      XinHeToast.center('打开链接失败！');
    }
  }

  requestAdoptSure() async {
    var userId = await XinHeLocalStorage.get(XinHeStorage.userId);
    Map<String,dynamic> ss = {
      // 'userId':userId,
      'adoptedTreeId':524827956560789504,
      'num':state.count.value,

    };

    XinHeRequestDataResult result = await XinHeHttpRequestData.requestUniversalPost(url: '/adoptedTreeOrder/sureAdoptedTree',params: ss,isPostAndwwwForm: true);
    if(!result.result || result.data['code'] != 0) {
      XinHeToast.center(result.data['message']);
      return ;
    }

    XinHeAdoptTreeModelEntity model = XinHeAdoptTreeModelEntity.fromJson(result.data['data']);


    Get.back();
    Get.toNamed(XinHeRoutes.adoptPayment,arguments: model);


  }
}
